<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
  </head>

  <body>
    <div id="app">
      <div class="m-3 row">
        <div class="col-9">
          <input type="text" v-model="title" class="form-control" />{{title}}
        </div>
        <div class="col-3">
          <button @click="addList" class="btn btn-primary mx-1">添加</button>
        </div>
      </div>
      <div class="m-2">
        <button
          class="mx-1 btn btn-outline-info btn-sm"
          @click="filterList(0)"
          v-bind:class="{active:(activeStatus===0)}"
        >
          全部
        </button>
        <button
          class="mx-1 btn btn-outline-secondary btn-sm"
          @click="()=>filterList(1)"
          v-bind:class="{active:(activeStatus===1)}"
        >
          未完成
        </button>
        <button
          class="mx-1 btn btn-outline-warning btn-sm"
          :class="{active:(activeStatus===2)}"
          @click="()=>filterList(2)"
        >
          进行中
        </button>
        <button
          class="mx-1 btn btn-outline-primary btn-sm"
          :class="{active:(activeStatus===3)}"
          @click="()=>filterList(3)"
        >
          已完成
        </button>
      </div>
      <table class="table">
        <thead>
          <tr>
            <th>名称</th>
            <th>状态</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="item in listArr"
            :key="item.id"
            :class="statusBg[item.status]"
          >
            <td>{{item.title}}</td>
            <td>{{status[item.status]}}</td>
            <td>
              <button
                @click="delList(item.id)"
                class="mx-1 btn btn-danger btn-sm"
              >
                删除
              </button>
              <button
                class="mx-1 btn btn-secondary btn-sm"
                @click="editStatus(item.id,1)"
                v-show="item.status!=='1'"
              >
                未完成
              </button>
              <button
                class="mx-1 btn btn-warning btn-sm"
                @click="editStatus(item.id,2)"
                v-show="item.status!=='2'"
              >
                进行中
              </button>
              <button
                class="mx-1 btn btn-primary btn-sm"
                @click="editStatus(item.id,3)"
                v-show="item.status!=='3'"
              >
                已完成
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <script type="module">
      import { createApp, ref, reactive, computed } from "./vue.esm-browser.js";
      import PocketBase from "./pocketbase.es.js";
      const pb = new PocketBase("http://127.0.0.1:8090");
      const app = createApp({
        setup() {
          const listArr = ref([]);
          const title = ref("");
          const status = {
            1: "未完成",
            2: "进行中",
            3: "已完成",
          };
          const statusBg = {
            1: "table-secondary",
            2: "table-warning",
            3: "table-primary",
          };
          // 按钮点击，设置状态
          const activeStatus = ref(1);
          function filterList(status) {
            activeStatus.value = status;
            getList();
          }

          // 添加
          async function addList() {
            if (title.value === "") return;
            await pb.collection("todolist").create({
              title: title.value,
              status: 1,
            });
            title.value = "";
            getList();
          }

          // 删除
          async function delList(id) {
            if (confirm("确定删除吗?")) {
              await pb.collection("todolist").delete(id);
              getList();
            }
          }

          // 修改状态
          async function editStatus(id, status) {
            await pb.collection("todolist").update(id, {
              status,
            });
            getList();
          }
          // ------------

          async function getList() {
            let filter = `status='${activeStatus.value}'`;
            if (activeStatus.value === 0) {
              filter = "";
            }
            const records = await pb
              .collection("todolist")
              .getList(1, 50, { filter });
            listArr.value = records.items;
          }
          getList();

          return {
            listArr,
            status,
            filterList,
            activeStatus,
            title,
            addList,
            delList,
            editStatus,
            statusBg,
          };
        },
      });

      app.mount("#app"); //挂载
    </script>
  </body>
</html>
